package com.zscat.mallplus.qsg.contrller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgRecharge;
import com.zscat.mallplus.qsg.service.IDeviceService;
import com.zscat.mallplus.qsg.service.IRechargeService;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
@Api(tags = "DeviceController", description = "充值管理")
@RequestMapping("/admin/qsg/recharge")
public class RechargeController {

    @Resource
    private IRechargeService rechargeService;

    @SysLog(MODULE = "qsg", REMARK = "根据条件查询所有充值列表")
    @ApiOperation("根据条件查询所有充值列表")
    @GetMapping(value = "/list")
    public Object getRechargeByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("create_datetime");
        qw.eq("store_id", UserUtils.getCurrentMember().getQsgStoreId());
        try {
            IPage<QsgRecharge> rechargeIPage = rechargeService.page(new Page<>(pageNum, pageSize), qw);
            return new CommonResult().success(rechargeIPage);
        } catch (Exception e) {
            log.error("根据条件查询所有充值列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "qsg", REMARK = "保存充值表")
    @ApiOperation("保存充值表")
    @PostMapping(value = "/create")
    public Object saveRecharge(@RequestBody QsgRecharge entity) {
        try {
            entity.setStoreId(UserUtils.getCurrentMember().getQsgStoreId().intValue());
            entity.setCreateDatetime(new Date());
            if (rechargeService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存充值表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "更新充值表")
    @ApiOperation("更新充值表")
    @PostMapping(value = "/update/{id}")
    public Object updateRecharge(@RequestBody QsgRecharge entity) {
        try {
            if (rechargeService.updateById(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新充值表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "删除充值表")
    @ApiOperation("删除充值表")
    @GetMapping(value = "/delete/{id}")
    public Object deleteRecharge(@ApiParam("充值表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("充值表id");
            }
            if (rechargeService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除充值表：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "qsg", REMARK = "给充值表分配充值表")
    @ApiOperation("查询充值表明细")
    @GetMapping(value = "/{id}")
    public Object getRechargeById(@ApiParam("充值表id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("充值表id");
            }
            QsgRecharge recharge = rechargeService.getById(id);
            return new CommonResult().success(recharge);
        } catch (Exception e) {
            log.error("查询充值表明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }
}
